package cn.ubest.mvvmcore.utils;


import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;

import cn.ubest.mvvmcore.R;


/**
 * Created by maoqi on 2020/4/26.
 */
public class DialogUtils {

    /**
     * 默认PositiveButton为"确认"，默认NegativeButton为"取消"，点击为dialog消失
     *
     * @param context          context
     * @param message          message
     * @param positiveListener "确认的点击事件"
     * @return
     */
    public static AlertDialog showDialog(Context context,
                                         @StringRes int message,
                                         DialogInterface.OnClickListener positiveListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton(R.string.confirm, positiveListener)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }

    /**
     * 默认PositiveButton为"确认"，默认NegativeButton为"取消"，点击为dialog消失
     *
     * @param context          context
     * @param message          message
     * @param positiveListener "确认的点击事件"
     * @return
     */
    public static AlertDialog showDialog(Context context,
                                         String message,
                                         DialogInterface.OnClickListener positiveListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton(R.string.confirm, positiveListener)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }

    /**
     * @param context          context
     * @param message          message
     * @param positiveText     "确认按钮的文字"
     * @param positiveListener "确认的点击事件"
     * @return
     */
    public static AlertDialog showDialog(Context context,
                                         String message,
                                         String positiveText,
                                         DialogInterface.OnClickListener positiveListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton(positiveText, positiveListener)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }

    /**
     * 默认PositiveButton为"确认"，默认NegativeButton为"取消"，点击为dialog消失
     *
     * @param context          context
     * @param message          message
     * @param positiveListener "确认的点击事件"
     * @return
     */
    public static AlertDialog showDialog(Context context,
                                         String message,
                                         DialogInterface.OnClickListener positiveListener,
                                         DialogInterface.OnClickListener negativeListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setCancelable(false)
                .setPositiveButton(R.string.confirm, positiveListener)
                .setNegativeButton(R.string.cancel, negativeListener)
                .show();
    }

    public static AlertDialog showDialog(Context context,
                                         @StringRes int message,
                                         DialogInterface.OnClickListener positiveListener,
                                         DialogInterface.OnClickListener negativeListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setCancelable(false)
                .setPositiveButton(R.string.confirm, positiveListener)
                .setNegativeButton(R.string.cancel, negativeListener)
                .show();
    }

    /**
     * 构造一个dialog
     *
     * @param context          context
     * @param message          message
     * @param positiveText
     * @param positiveListener
     * @param negativeText
     * @param negativeListener
     * @return
     */
    public static AlertDialog showDialog(Context context,
                                         String message,
                                         String positiveText,
                                         DialogInterface.OnClickListener positiveListener,
                                         String negativeText,
                                         DialogInterface.OnClickListener negativeListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton(positiveText, positiveListener)
                .setNegativeButton(negativeText, negativeListener)
                .show();
    }

    /**
     * 单按钮的dialog
     *
     * @param context
     * @param message
     * @param buttonText
     * @param onClickListener
     * @return
     */
    public static AlertDialog showSingleButtonDialog(Context context,
                                                     String message,
                                                     String buttonText,
                                                     DialogInterface.OnClickListener onClickListener) {
        return new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton(buttonText, onClickListener)
                .show();
    }

    /**
     * 构造一个items dialog
     *
     * @param context  context
     * @param titles   items titles
     * @param listener 选择监听
     * @return
     */
    public static AlertDialog showItemsDialog(Context context, String[] titles, DialogInterface.OnClickListener listener) {
        return new AlertDialog.Builder(context)
                .setItems(titles, listener)
                .show();
    }

    public static AlertDialog showDialog(Context context, View view) {
        return new AlertDialog.Builder(context)
                .setView(view)
                .show();
    }
}
